﻿using Microsoft.Xna.Framework.Graphics;

namespace BlockeyEssentials.ScreenSystem
{
    public abstract class Screen
    {
        public Screen(ScreenManager oManager)
        {
            Visible = true;
            Enabled = true;
            Transition = 0.0f;
            TransitionRate = (1f / 60f) * 4;

            _Manager = oManager;
            _DestinationScreen = null;
            _DestinationManager = null;
        }

        #region Methods

        /// <summary>
        /// Updates any logic within the screen
        /// </summary>
        /// <param name="oManager">The Current ScreenManager</param>
        public virtual void Update()
        { }

        /// <summary>
        /// Draws the Screen onto the Screen
        /// </summary>
        /// <param name="oSB">The Spritebatch Used for the screen</param>
        public virtual void Draw(SpriteBatch oSB)
        { }

        /// <summary>
        /// Defines what to do after this screen is fulled added
        /// </summary>
        /// <param name="oManager">The Current ScreenManager</param>
        public virtual void ActionWhenFullyAdded()
        { }

        /// <summary>
        /// Defines what to do after this screen is fulled removed
        /// </summary>
        /// <param name="oManager">The Current ScreenManager</param>
        public virtual void ActionWhenFullyRemoved()
        {
            if (_DestinationManager != null)
            {
                _Manager.RemoveAllScreens();
                All.DestinationScreenManager = _DestinationManager;
                _DestinationManager = null;
            }
            if (_DestinationScreen != null)
            {
                _Manager.AddScreen(_DestinationScreen);
                _DestinationScreen = null;
            }
        }

        protected void GoTo(Screen oScreen)
        {
            Remove();
            _DestinationScreen = oScreen;
        }

        public void Remove()
        {
            _Manager.RemoveScreen(this);
        }

        #endregion

        #region Properties

        /// <summary>
        /// Returns whether the screen can be visible or not. If set to false, it will also be disabled
        /// </summary>
        public bool Visible
        {
            get { return _Visible; }
            set
            {
                _Visible = value;
                if (_Visible == false)
                    _Enabled = false;
            }
        }

        /// <summary>
        /// Returns whether the screen can be updated or not
        /// </summary>
        public bool Enabled
        {
            get { return _Enabled; }
            set { _Enabled = value; }
        }

        /// <summary>
        /// Returns whether the screen is fulled transitioned or not
        /// </summary>
        public bool Active
        {
            get { return _Transition >= 1.0f; }
        }

        /// <summary>
        /// Returns the current point of transition
        /// </summary>
        public float Transition
        {
            get { return _Transition; }
            set { _Transition = value; }
        }

        /// <summary>
        /// Returns the rate at which the screen will transition
        /// </summary>
        public float TransitionRate
        {
            get { return _TransitionRate; }
            set { _TransitionRate = value; }
        }

        #endregion

        private bool _Visible;
        private bool _Enabled;
        private float _Transition;
        private float _TransitionRate;

        protected ScreenManager _Manager;
        protected Screen _DestinationScreen;
        protected ScreenManager _DestinationManager;

    }
}
